package flc.ast.fragment;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.Utils;

import java.io.File;

import flc.ast.R;
import flc.ast.activity.PicConvertActivity;
import flc.ast.databinding.FragmentPicMusicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PicMusicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PicMusicFragment extends BaseNoModelFragment<FragmentPicMusicBinding> {

    protected static final int REQ_MUSIC = 1;

    private String selPath="";

    @Override
    protected int onCreate() {
        return R.layout.fragment_pic_music;
    }

    @Override
    protected void initView() {
        mDataBinding.llAddMusic.setOnClickListener(this);
        mDataBinding.ivMusicDel.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.llAddMusic:
                IntentUtil.pickAudio(PicMusicFragment.this,REQ_MUSIC);
                break;
            case R.id.ivMusicDel:
                setMusic(null);
                mDataBinding.tvMusicName.setText(getString(R.string.add_music));
                mDataBinding.tvMusicName.setTextColor(Color.parseColor("#FF8C8C8C"));
                mDataBinding.ivAddMusic.setImageResource(R.drawable.icon_tj);
                mDataBinding.ivMusicDel.setVisibility(View.GONE);
                mDataBinding.llAddMusic.setBackgroundResource(R.drawable.shape_splice_add);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQ_MUSIC) {
                Uri musicUri = data.getData();
                if (musicUri != null) {
                    String fileName = null;
                    Cursor cursor = getActivity().getContentResolver().query(musicUri, null, null, null, null);
                    if (cursor != null && cursor.moveToFirst()) {
                        int displayNameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        fileName = cursor.getString(displayNameIndex);
                        cursor.close();
                    }
                    mDataBinding.tvMusicName.setText(fileName);
                    mDataBinding.tvMusicName.setTextColor(Color.parseColor("#FF202020"));
                    mDataBinding.ivAddMusic.setImageResource(R.drawable.icon_yy);
                    mDataBinding.ivMusicDel.setVisibility(View.VISIBLE);
                    mDataBinding.llAddMusic.setBackgroundResource(R.drawable.shape_speed1);
                    setMusic(musicUri);
                }
            }
        }
    }

    /**
     * 设置音乐
     *
     * @param musicUri
     */
    protected void setMusic(Uri musicUri) {
        Log.e(TAG, "setMusic: " );
        if (musicUri == null) return;
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                PicConvertActivity.mMoviePresenter.setMusic(musicUri);
                emitter.onNext(true);
            }

            @Override
            public void accept(Boolean obj) {
                PicConvertActivity.mMoviePresenter.setTransfer(PicConvertActivity.mMoviePresenter.getMovieType());
                dismissDialog();
            }
        });
    }

}